package com.sgb.kjwl.view.ui.adapter.self;

import android.content.Context;
import androidx.appcompat.widget.AppCompatRadioButton;

import android.widget.CompoundButton;
import android.widget.TextView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.user.ReceiptEntity;
import com.sgb.kjwl.view.ui.activity.self.publish.ReceiptInfoEditActivity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

public class ReceiptInfoAdapter extends BaseRecycleAdapter<ReceiptEntity> {

    private TextView mDeleteTv;
    private TextView mEditTv;
    private AppCompatRadioButton appCompatRadioButton;
    private IClick iClick;

    public ReceiptInfoAdapter(Context context, List<ReceiptEntity> datas) {
        super(context, R.layout.item_receipt_info, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, ReceiptEntity receiptEntity) {
        holder.setBinding(BR.viewModel, receiptEntity);
        mDeleteTv = holder.itemView.findViewById(R.id.tv_del);
        mEditTv = holder.itemView.findViewById(R.id.tv_edit);
        appCompatRadioButton = holder.itemView.findViewById(R.id.radio_is_def);
        if (receiptEntity.isDefault() == true) {
//            tags.add("默认");
            appCompatRadioButton.setChecked(true);
            appCompatRadioButton.setText("已设为默认");
        } else {
            appCompatRadioButton.setChecked(false);
            appCompatRadioButton.setText("设为默认");
        }
        //删除
        mDeleteTv.setOnClickListener(view -> {
            iClick.onDelete(receiptEntity.getId(), holder.getAdapterPosition());
        });
        //编辑
        mEditTv.setOnClickListener(view -> {
            ReceiptInfoEditActivity.start(context, receiptEntity);
        });
        appCompatRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (!b) {
                    return;
                }
                iClick.isCheck(b,holder.getPosition()-1);
            }
        });
    }

    public interface IClick {
        void onDelete(String id, int position);
        void isCheck(boolean isCheck, int position);
    }

    public void IClick(IClick iClick) {
        this.iClick = iClick;
    }
}
